<template>
    <div>
        <Category :scene="scene"></Category>
        <el-card style="margin: 10px 0;">
            <div v-show="scene == 0">
                <el-button type="primary" @click="addSpu" icon="Plus"
                    :disabled="categoryStore.c3Id ? false : true">添加SPU</el-button>
                <el-table border style="margin: 10px 0 ;" :data="record">
                    <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
                    <el-table-column label="SPU名称" prop="spuName"></el-table-column>
                    <el-table-column label="SPU描述" prop="description" show-overflow-tooltip></el-table-column>
                    <el-table-column label="SPU操作">
                        <template #="{ row, $index }">
                            <el-button type="primary" @click="addSku(row)" icon="Plus" title="添加SKU"></el-button>
                            <el-button type="primary" @click="updateSpu(row)" icon="Edit" title="修改SPU"></el-button>
                            <el-button type="primary" @click="findSku(row)" icon="View" title="查看SKU列表"></el-button>
                            <el-popconfirm :title="`你确定删除${row.spuName}吗？`" width="200px" @confirm="deleteSpu(row)">
                                <template #reference>
                                    <el-button type="primary" @click="" icon="Delete" title="删除SPU"></el-button>
                                </template>
                            </el-popconfirm>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination v-model:current-page="pageNo" v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 9,]"
                    :background="true" layout="prev, pager, next, jumper,->, sizes,total " :total="total"
                    @current-change="getHasSpu" @size-change="changeSize" />
            </div>
            <!-- 添加和修改spu的子组件 -->
            <SpuForm v-show="scene == 1" @changeScene="changeScene" ref="spuForm" />
            <!-- 添加sku的子组件 -->
            <SkuForm v-show="scene == 2" @changeScene="changeScene" ref="sku" />
            <!-- dialog对话框：展示sku数据 -->
            <el-dialog title="SKU列表" v-model="show">
                <el-table border :data="skuArr">
                    <el-table-column label="SKU名字" prop="skuName"></el-table-column>
                    <el-table-column label="SKU价格" prop="price"></el-table-column>
                    <el-table-column label="SKU重量" prop="weight"></el-table-column>
                    <el-table-column label="SKU图片">
                        <template #="{ row, $index }">
                            <img :src="row.skuDefaultImg" alt="" style="width: 100px;height: 100px;">
                        </template>
                    </el-table-column>
                </el-table>
            </el-dialog>
        </el-card>
    </div>
</template>

<script setup lang='ts' name="Spu">
import { ref, watch, onBeforeUnmount } from 'vue'
import { reqHasSup, reqSkuList, reqRemoveSpu } from '@/api/product/spu';
import useCategoryStore from '@/store/modules/category';
import type { HasSpuResponseData, Records } from '@/api/product/spu/type';
import SpuForm from './spuForm.vue';
import SkuForm from './skuForm.vue';
import type { SupData } from '@/api/product/spu/type'
import { ElMessage } from 'element-plus';
let categoryStore = useCategoryStore();
let scene = ref<number>(0)//0显示已有spu 1：添加或修改spu 2：添加sku
let pageNo = ref<number>(1);
let pageSize = ref<number>(3)
let total = ref<number>(0)
let record = ref<Records>([])
let spuForm = ref<any>()
let sku = ref<any>()
//存储全部的sku数据
let skuArr = ref<any>([])
let show = ref<boolean>(false)
watch(() => categoryStore.c3Id, () => {
    if (!categoryStore.c3Id) return
    getHasSpu();
});
const getHasSpu = async (pager = 1) => {
    pageNo.value = pager
    let result: HasSpuResponseData = await reqHasSup(pageNo.value, pageSize.value, categoryStore.c3Id)
    if (result.code == 200) {
        record.value = result.data.records
        total.value = result.data.total
    }

}
const changeSize = () => {
    getHasSpu()
}
//添加SPu
const addSpu = () => {
    //点击切换到spuform
    scene.value = 1
    spuForm.value.initAddSpu()
}
// 子组件绑定的自定义事件
const changeScene = (obj: any) => {
    scene.value = obj.flag
    if (obj.params == 'update') {
        getHasSpu(pageNo.value);
    } else {
        getHasSpu();
    }
}
//修改Spu
const updateSpu = (row: SupData) => {
    //点击切换到spuform
    scene.value = 1
    //调用子组件实例方法获取完整已有的SPU数据
    spuForm.value.initHasSpuData(row);
}
//添加sku
const addSku = (row: SupData) => {
    scene.value = 2
    //调用子组件方法初始化添加sku数据
    sku.value.initSkuData(categoryStore.c1Id, categoryStore.c2Id, row);
}
//查看列表
const findSku = async (row: any) => {
    let result = await reqSkuList(row.id)
    if (result.code == 200) {
        skuArr.value = result.data
        show.value = true
    }
}
//删除
const deleteSpu = async (row: any) => {
    let result: any = await reqRemoveSpu(row.id)
    if (result.code == 200) {
        ElMessage({
            type: 'success',
            message: '删除成功'
        })
        getHasSpu(record.value.length > 1 ? pageNo.value : pageNo.value - 1)
    }
    else {
        ElMessage({
            type: 'error',
            message: '删除失败'
        })
    }
}
//组件销毁前，清孔仓库关于分类的数据
onBeforeUnmount(() => {
    categoryStore.$reset();
})
</script>

<style></style>